package com.example.administrator.android2019.cg6service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.android2019.R;

import static com.example.administrator.android2019.R.id.ch6_service2_btn1;

public class ch6_service2Activity extends AppCompatActivity implements View.OnClickListener {


  Button btn_bind,btn_unbind;
   MyConn myConn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch6_service2);
        Button btn_start, btn_stop;
        btn_bind = findViewById(R.id.ch6_service2_btn1);
        btn_unbind = findViewById(R.id.ch6_service2_btn2);
        btn_unbind.setOnClickListener(this);
        btn_bind.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ch6_service2_btn1:

                Intent intent=new Intent(this,MyService2.class);
               bindService(intent,myConn,BIND_ABOVE_CLIENT);
                /*Toast:参数1 上下文（当前Activity）；
                 * 参数2：显示内容
                 * 参数3：显示时间（长短）
                 * */
                Toast.makeText(this, "服务已绑定", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ch6_service2_btn2:
             if (myConn!=null){
                 unbindService(myConn);
                 myConn=null;
                 Toast.makeText(this, "服务已解绑", Toast.LENGTH_SHORT).show();
             }
              break;

        }
    }

    class MyConn implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
          //  Log.v("myService","Activity中的");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}